using System.Collections;
using UnityEngine;

public class Background : MonoBehaviour
{
	public static Background instance;

	public Transform TF_Backgound;

	public Transform TF_Floor;

	public SpriteRenderer SR_Planet;

	public Transform TF_Golem;

	public SpriteRenderer[] SR_BackgoundList;

	public ParticleSystem[] PS_WeatherList;

	private float currentFloorPosX;

	private Coroutine co;

	private void Awake()
	{
		instance = this;
		ChangeDayAndNight();
		ChangeWeatherRainAndSnow();
	}

	public void Init()
	{
		FloorMoving();
	}

	public void FloorMoving()
	{
		TF_Floor.localPosition = Vector2.zero;
		if (co != null)
		{
			StopCoroutine(co);
		}
		co = StartCoroutine(IE_FloorMoving());
	}

	private IEnumerator IE_FloorMoving()
	{
		while (true)
		{
			float temp1 = (int)(Cam.instance.tf.position.x / 54f);
			float temp2 = (int)(TF_Floor.position.x / 54f);
			if (temp1 != temp2)
			{
				TF_Floor.localPosition = new Vector2(temp1 * 54f, 0f);
			}
			yield return new WaitForSeconds(0.2f);
		}
	}

	public void ChangeDayAndNight()
	{
		StartCoroutine(IE_ChangeDayAndNight());
	}

	private IEnumerator IE_ChangeDayAndNight()
	{
		Color c = SR_BackgoundList[1].color;
		while (true)
		{
			yield return new WaitForSeconds(20f);
			for (float f = 0f; f < 1f; f += Time.fixedDeltaTime * 0.1f)
			{
				SR_BackgoundList[1].color = new Color(c.r, c.g, c.b, f);
				yield return new WaitForFixedUpdate();
			}
			yield return new WaitForSeconds(20f);
			for (float f2 = 1f; f2 > 0f; f2 -= Time.fixedDeltaTime * 0.1f)
			{
				SR_BackgoundList[1].color = new Color(c.r, c.g, c.b, f2);
				yield return new WaitForFixedUpdate();
			}
		}
	}

	public void ChangeWeatherRainAndSnow()
	{
		StartCoroutine(IE_ChangeWeatherRainAndSnow());
	}

	private IEnumerator IE_ChangeWeatherRainAndSnow()
	{
		yield return new WaitForSeconds(Random.Range(10, 300));
		while (true)
		{
			int rainOrSnow = Random.Range(0, 2);
			PS_WeatherList[rainOrSnow].Play();
			if (rainOrSnow == 0)
			{
				Audio.instance.sfxLoop[1].Play();
			}
			yield return new WaitForSeconds(Random.Range(200, 400));
		}
	}
}
